package com.jones.restaurant.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jones.restaurant.beans.Menu;

public class SortBeanUtils {
	static List<Integer> courseIdList;
	static Map<Integer,List<Menu>> datamap;
	static{
		courseIdList = new ArrayList<Integer>();
		datamap = new HashMap<Integer, List<Menu>>();
	}
	public static List<Menu> getOrderlyList(){
		List<Menu> list = new ArrayList<Menu>();
		int count = 0;
		for(Map.Entry<Integer, List<Menu>> entry : datamap.entrySet()){
			for(int i = 0;i<entry.getValue().size();i++){
				entry.getValue().get(i).setRight2left(count);
			}
			list.addAll(list.size(), entry.getValue());
			count++;
		}
		return list;
		
	}
	
	
	public static List<Menu> getLeftOrderlyList(){
		List<Menu> list = new ArrayList<Menu>();
		int count = 0;
		for(Map.Entry<Integer, List<Menu>> entry : datamap.entrySet()){
			entry.getValue().get(0).setLeft2right(count);
			list.add(entry.getValue().get(0));
			count+=entry.getValue().size();
		}
		return list;
	}
	
	public static void addMenu(Menu menu){
		if(courseIdList.contains(menu.getCourse_category_id())){
			datamap.get(menu.getCourse_category_id()).add(menu);
		}else{
			courseIdList.add(menu.getCourse_category_id());
			List<Menu> tempList = new ArrayList<Menu>();
			tempList.add(new Menu(menu.getCourse_category_id(), menu.getCourse_category_name()));
			tempList.add(menu);
			datamap.put(menu.getCourse_category_id(), tempList);
		}
	}
	
	public static void clearAllData(){
		courseIdList.clear();
		datamap.clear();
	}
}
